package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McInfoListMapper;
import cc.rengu.igas.momp.common.dao.MchntInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.McInfoListMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.MchntInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.MoMcListInfoInObj;
import cc.rengu.igas.momp.common.entity.MoMcListInfoOutObj;
import cc.rengu.igas.momp.common.entity.TBMchntBaseInfo;
import cc.rengu.igas.momp.common.entity.TMPlatBizTmp;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.model.bean.JsonTmpMoMcInfo;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.MoneyOffMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMoneyOffMcListInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryMoneyOffMcListInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 减免类活动列表查询功能实现
 *
 * @author Jinan Liu
 * @date 2020/3/6 17:59
 */
public class QueryMoneyOffMcListService extends RadpService {

    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作类型
     */
    private String globalQueryType;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 操作角色
     */
    private String globalQueryRole;
    /**
     * 操作人所属机构
     */
    private String globalQueryOrgId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryMoneyOffMcListInfoRequest queryMoneyOffMcListInfoRequest = new QueryMoneyOffMcListInfoRequest();
            ConvertUtil.convertOutput(queryMoneyOffMcListInfoRequest);

            QueryMoneyOffMcListInfoResponse queryMoneyOffMcListInfoResponse = new QueryMoneyOffMcListInfoResponse();
            queryMoneyOffMcListInfoResponse.setHeader(queryMoneyOffMcListInfoRequest.getHeader());
            BizResponse<QueryMoneyOffMcListInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryMoneyOffMcListInfoResponse);

            /* 报文检查 */
            messageValidation(queryMoneyOffMcListInfoRequest);

            rglog.info("机构<{}>在<{}>由<{}>开始查询减免类营销活动列表信息流程", globalInstId, globalTimeStamp, globalQueryId);

            // 服务调用
            BizResponse<QueryMoneyOffMcListInfoResponse> bizResponseNew = queryMoneyOffMcListInfo(xmlTreeUtil, queryMoneyOffMcListInfoRequest, queryMoneyOffMcListInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询减免类营销活动列表信息
     *
     * @param xmlTreeUtil                     内部XML树
     * @param queryMoneyOffMcListInfoRequest  减免类活动列表查询接口请求对象
     * @param queryMoneyOffMcListInfoResponse 减免类营销活动列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryMoneyOffMcListInfoResponse> 减免类营销活动列表查询接口应答对象
     * @throws BizException 业务异常信息
     * @author Jinan Liu
     * @date 2020/5/27 22:41
     */
    private BizResponse<QueryMoneyOffMcListInfoResponse> queryMoneyOffMcListInfo(XmlTreeUtil xmlTreeUtil, QueryMoneyOffMcListInfoRequest queryMoneyOffMcListInfoRequest, QueryMoneyOffMcListInfoResponse queryMoneyOffMcListInfoResponse)
            throws Exception {

        BizResponse<QueryMoneyOffMcListInfoResponse> bizResponse = new BizResponse<>();

        List<MoneyOffMcListInfoBean> moneyOffMcListInfoBeanList;

        rglog.debug("当前是<{}>查询数据", QueryRoleEnum.getByValue(globalQueryRole).getQueryRoleDesc());

        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType) && QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

            /* 银行审批岗因为数据出处原因单独处理 */
            moneyOffMcListInfoBeanList = processTmpMoMcListInfo(queryMoneyOffMcListInfoRequest);
            if (null == moneyOffMcListInfoBeanList || moneyOffMcListInfoBeanList.isEmpty()) {

                rglog.info("根据查询条件查询到0条减免类营销活动信息!");
                queryMoneyOffMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryMoneyOffMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                queryMoneyOffMcListInfoResponse.setTotalRows(String.valueOf(moneyOffMcListInfoBeanList.size()));
                if (CommonConstant.ZERO == (moneyOffMcListInfoBeanList.size() % Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize()))) {
                    queryMoneyOffMcListInfoResponse.setTotalPage(String.valueOf(moneyOffMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize()))));
                } else {
                    queryMoneyOffMcListInfoResponse.setTotalPage(String.valueOf(moneyOffMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize())) + 1));
                }

                /* 分页查询 */
                int fromIndex = Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageIndex()) * Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize());
                int endIndex = fromIndex + Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize());
                if (fromIndex <= moneyOffMcListInfoBeanList.size()) {
                    List<MoneyOffMcListInfoBean> moneyOffMcListInfoBeanList1 = moneyOffMcListInfoBeanList.subList(fromIndex, Math.min(endIndex, moneyOffMcListInfoBeanList.size()));
                    queryMoneyOffMcListInfoResponse.setMcListInfo(moneyOffMcListInfoBeanList1);
                }
            }

        } else {

            /* 数据库入参赋值 */
            MoMcListInfoInObj moMcListInfoInObj = setupDatabaseInputObject(queryMoneyOffMcListInfoRequest);

            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageIndex()));
            pageInfo.setPageSize(Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize()));
            rglog.info("当前待处理页数为<{}>,每页记录数<{}>", queryMoneyOffMcListInfoRequest.getPageIndex(), queryMoneyOffMcListInfoRequest.getPageSize());

            List<MoMcListInfoOutObj> moMcListInfoOutObjList = new ArrayList<>();
            PageResult pageResult;

            McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();

            /* 根据角色权限查询数据 */
            switch (QueryTypeEnum.getByValue(globalQueryType)) {
                case BANK_QUERY_TYPE:
                    /* 银行人员,需要拼接营销活动机构信息表查询 */

                    /* 机构编号 */
                    moMcListInfoInObj.setOrgId(globalQueryOrgId);

                    switch (QueryRoleEnum.getByValue(globalQueryRole)) {
                        case BANK_BASIC_QUERY_ROLE:
                            /* 银行录入岗 */
                            pageResult = mcInfoListMapper.selectMoneyOffMcListInfoByBankBasicRole(moMcListInfoInObj, pageInfo);
                            break;
                        case BANK_PUBLISH_QYERY_ROLE:
                            /* 银行发布岗 */
                            pageResult = mcInfoListMapper.selectMoneyOffMcListInfoByBankPublishRole(moMcListInfoInObj, pageInfo);
                            break;
                        default:
                            /* 其他查询角色不支持 */
                            rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                    }

                    if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    }

                    rglog.debug("SIZE=<{}>", pageResult.getResult().size());

                    break;

                case MCHNT_QYERY_TYPE:
                    /* 商户人员,需要拼接营销活动商户信息表查询 */

                    /* 根据商户编号查询商圈编号 */
                    String mchntNo = queryMoneyOffMcListInfoRequest.getQueryId();
                    String mchntArea = null;
                    TBMchntBaseInfo tbMchntBaseInfo;
                    if (!StringUtil.isNullorEmpty(mchntNo)) {
                        /* 查询商户基本信息表 */
                        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
                        tbMchntBaseInfo = mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
                        if (null == tbMchntBaseInfo) {
                            rglog.error("查询法人机构<{}>商户<{}>基本信息失败!", globalInstId, mchntNo);
                            throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                        } else {
                            /* 商户编号 */
                            if (!StringUtil.isNullorEmpty(mchntNo)) {
                                moMcListInfoInObj.setMcNo(mchntNo);
                            }

                            /* 商圈编号 */
                            mchntArea = tbMchntBaseInfo.getMchntArea();
                            if (!StringUtil.isNullorEmpty(mchntArea)) {
                                moMcListInfoInObj.setMchntArea(mchntArea);
                            }
                        }
                    }

                    if (StringUtil.isNullorEmpty(mchntArea)) {
                        pageResult = mcInfoListMapper.selectMoneyOffMcListInfoByMchnt(moMcListInfoInObj, pageInfo);
                    } else {
                        pageResult = mcInfoListMapper.selectMoneyOffMcListInfoByMchntWithArea(moMcListInfoInObj, pageInfo);
                    }
                    if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    }

                    break;

                default:
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条减免类营销活动信息!");

                queryMoneyOffMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryMoneyOffMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                for (Object object : pageResult.getResult()) {
                    moMcListInfoOutObjList.add((MoMcListInfoOutObj) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());

                moneyOffMcListInfoBeanList = moMcListInfoOutObjList.stream().map(this::convertMcMoneyOffInfo).collect(Collectors.toList());
                queryMoneyOffMcListInfoResponse.setMcListInfo(moneyOffMcListInfoBeanList);

                queryMoneyOffMcListInfoResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryMoneyOffMcListInfoResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

            }
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryMoneyOffMcListInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryMoneyOffMcListInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryMoneyOffMcListInfoResponse.setPageIndex(queryMoneyOffMcListInfoRequest.getPageIndex());
        queryMoneyOffMcListInfoResponse.setPageSize(queryMoneyOffMcListInfoRequest.getPageSize());

        bizResponse.setResult(queryMoneyOffMcListInfoResponse);

        return bizResponse;
    }


    /**
     * 请求报文体内容检查
     *
     * @param queryMoneyOffMcListInfoRequest 减免类活动列表查询接口请求对象
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryMoneyOffMcListInfoRequest queryMoneyOffMcListInfoRequest) {

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryMoneyOffMcListInfoRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryMoneyOffMcListInfoRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryMoneyOffMcListInfoRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryMoneyOffMcListInfoRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryMoneyOffMcListInfoRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getPageIndex())) {
            rglog.error("当前页数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageIndex = Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageIndex());
            if (CommonConstant.ZERO > pageIndex) {
                rglog.error("当前查询页数必须大于0! PAGE_INDEX=<{}>", pageIndex);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getPageSize())) {
            rglog.error("页显示记录数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageSize = Integer.parseInt(queryMoneyOffMcListInfoRequest.getPageSize());
            if (CommonConstant.ONE > pageSize) {
                rglog.error("每页记录数必须大于等于1! PAGE_SIZE=<{}>", pageSize);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }


    /**
     * 数据库查询前赋值数据库实体类入参
     *
     * @param queryMoneyOffMcListInfoRequest 减免类活动列表查询接口请求对象
     * @return cc.rengu.igas.momp.common.entity.MoneyOffMcListInfoInObj 营销活动列表查询使用实体类入参
     * @author Jinan Liu
     * @date 2020/5/28 12:57
     */
    private MoMcListInfoInObj setupDatabaseInputObject(QueryMoneyOffMcListInfoRequest queryMoneyOffMcListInfoRequest) {

        MoMcListInfoInObj moMcListInfoInObj = new MoMcListInfoInObj();

        /* 机构编码 */
        moMcListInfoInObj.setInstId(globalInstId);

        /* 营销活动编号 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcNo())) {
            moMcListInfoInObj.setMcNo(queryMoneyOffMcListInfoRequest.getMcNo());
        }
        /* 营销活动名称 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcName())) {
            moMcListInfoInObj.setMcName(queryMoneyOffMcListInfoRequest.getMcName());
        }
        /* 营销活动类型 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcType())) {
            moMcListInfoInObj.setMcType(queryMoneyOffMcListInfoRequest.getMcType());
        }
        /* 营销活动状态 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcStatus())) {
            moMcListInfoInObj.setMcStatus(queryMoneyOffMcListInfoRequest.getMcStatus());
        }
        /* 审批状态 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getAuditStatus())) {
            moMcListInfoInObj.setAuditStatus(queryMoneyOffMcListInfoRequest.getAuditStatus());
        }
        /* 当前交易日期 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getHeader().getTransDate())) {
            moMcListInfoInObj.setTxnDate(queryMoneyOffMcListInfoRequest.getHeader().getTransDate());
        }

        return moMcListInfoInObj;
    }

    /**
     * 银行审批岗数据查询
     *
     * @param queryMoneyOffMcListInfoRequest 减免类活动列表查询接口请求对象
     * @return java.util.List<cc.rengu.igas.momp.facade.bean.MoneyOffMcListInfoBean> 减免类营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/20 16:53
     */
    private List<MoneyOffMcListInfoBean> processTmpMoMcListInfo(QueryMoneyOffMcListInfoRequest queryMoneyOffMcListInfoRequest) throws Exception {

        List<MoneyOffMcListInfoBean> moneyOffMcListInfoBeanList;

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setInstId(globalInstId);
        tmPlatBizTmp.setOrgId(globalQueryOrgId);
        tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());

        /* 营销活动编号 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcNo())) {
            tmPlatBizTmp.setMcNo(queryMoneyOffMcListInfoRequest.getMcNo());
        }

        /* 营销活动状态 */
        if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcStatus())) {
            tmPlatBizTmp.setMcStatus(queryMoneyOffMcListInfoRequest.getMcStatus());
        }

        /* 获取数据 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        List<TMPlatBizTmp> tmPlatBizTmpList = platBizTmpMapper.selectTmpMcListInfo(tmPlatBizTmp);
        if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {

            moneyOffMcListInfoBeanList = tmPlatBizTmpList.stream().map(this::convertTmpMcInfo).collect(Collectors.toList());

            rglog.debug("SIZE=<{}>", moneyOffMcListInfoBeanList.size());

            /* 代码过滤查询条件 */

            /* 按照营销活动名称筛选 */
            if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcName())) {
                moneyOffMcListInfoBeanList = moneyOffMcListInfoBeanList.stream().filter(item -> item.getMcName().contains(queryMoneyOffMcListInfoRequest.getMcName())).collect(Collectors.toList());
            }

            /* 按照营销活动类型筛选 */
            if (!StringUtil.isNullorEmpty(queryMoneyOffMcListInfoRequest.getMcType())) {
                moneyOffMcListInfoBeanList = moneyOffMcListInfoBeanList.stream().filter(item -> item.getMcType().equals(queryMoneyOffMcListInfoRequest.getMcType())).collect(Collectors.toList());
            }

            return moneyOffMcListInfoBeanList;

        } else {
            return null;
        }
    }

    /**
     * 数据库实体类和接口实体类数据转换
     *
     * @param moMcListInfoOutObj 营销活动列表查询使用实体类出参
     * @return cc.rengu.igas.momp.facade.bean.MoneyOffMcListInfoBean 减免类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/5/28 00:15
     */
    private MoneyOffMcListInfoBean convertMcMoneyOffInfo(MoMcListInfoOutObj moMcListInfoOutObj) {

        MoneyOffMcListInfoBean moneyOffMcListInfoBean = new MoneyOffMcListInfoBean();
        try {
            /* 营销活动编号 */
            moneyOffMcListInfoBean.setMcNo(moMcListInfoOutObj.getMcNo());
            /* 营销活动名称 */
            moneyOffMcListInfoBean.setMcName(moMcListInfoOutObj.getMcName());
            /* 营销活动类型 */
            moneyOffMcListInfoBean.setMcType(moMcListInfoOutObj.getMcType());
            /* 活动起始日期 */
            moneyOffMcListInfoBean.setMcStartDate(moMcListInfoOutObj.getMcStartDate());
            /* 活动截止日期 */
            moneyOffMcListInfoBean.setMcEndDate(moMcListInfoOutObj.getMcEndDate());
            /* 活动结束日期 */
            moneyOffMcListInfoBean.setMcActualEndDate(moMcListInfoOutObj.getMcActualEndDate());
            /* 出资方 */
            if (!StringUtil.isNullorEmpty(moMcListInfoOutObj.getSponsor())) {
                moneyOffMcListInfoBean.setSponsor(moMcListInfoOutObj.getSponsor());
            }
            /* 活动预算 */
            moneyOffMcListInfoBean.setBudget(moMcListInfoOutObj.getBudget());
            /* 已消耗预算 */
            if (StringUtil.isNullorEmpty(moMcListInfoOutObj.getTotalAmtAcc())) {
                moneyOffMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            } else {
                moneyOffMcListInfoBean.setBudgetUsed(moMcListInfoOutObj.getTotalAmtAcc());
            }
            /* 审批状态 */
            moneyOffMcListInfoBean.setAuditStatus(moMcListInfoOutObj.getAuditStatus());
            /* 创建人 */
            moneyOffMcListInfoBean.setOprId(moMcListInfoOutObj.getOprId());
            /* 创建时间 */
            moneyOffMcListInfoBean.setCreateTime(moMcListInfoOutObj.getCreateTime());
            /* 最后修改人 */
            if (StringUtil.isNullorEmpty(moMcListInfoOutObj.getLastOprId())) {
                moneyOffMcListInfoBean.setLastOprId("");
            } else {
                moneyOffMcListInfoBean.setLastOprId(moMcListInfoOutObj.getLastOprId());
            }
            /* 修改时间 */
            if (StringUtil.isNullorEmpty(moMcListInfoOutObj.getUpdateTime())) {
                moneyOffMcListInfoBean.setUpdateTime("");
            } else {
                moneyOffMcListInfoBean.setUpdateTime(moMcListInfoOutObj.getUpdateTime());
            }
            /* 查询主体参加活动标志 */
            moneyOffMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_DEFAULT);

        } catch (Exception e) {
            rglog.error("转换营销活动信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return moneyOffMcListInfoBean;
    }

    /**
     * 转换换业务数据临时表临时数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.MoneyOffMcListInfoBean 减免类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/20 15:37
     */
    private MoneyOffMcListInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {

        MoneyOffMcListInfoBean moneyOffMcListInfoBean = new MoneyOffMcListInfoBean();

        try {

            String mcNo = tmPlatBizTmp.getMcNo();
            String mcName;
            String mcType;
            String mcStartDate;
            String mcEndDate;
            String mcActualEndDate;
            String budget;
            String sponsor = null;

            /* 根据营销活动类型解析BIZ_DATA1字段内容 */
            String mcTypeOne = tmPlatBizTmp.getMcNo().substring(2, 3);
            if (McTypeEnum.MC_TYPE_MO.getMcTypeCode().equals(mcTypeOne)) {
                JsonTmpMoMcInfo jsonTmpMoMcInfo = JsonOperation.parseMoMcInfoTmpBizData1(tmPlatBizTmp, rglog);
                mcName = jsonTmpMoMcInfo.getMcName();
                mcType = jsonTmpMoMcInfo.getMcType();
                mcStartDate = jsonTmpMoMcInfo.getMcStartDate();
                mcEndDate = jsonTmpMoMcInfo.getMcEndDate();
                mcActualEndDate = jsonTmpMoMcInfo.getMcActualEndDate();
                if (!StringUtil.isNullorEmpty(jsonTmpMoMcInfo.getSponsor())) {
                    sponsor = jsonTmpMoMcInfo.getSponsor();
                }
                budget = jsonTmpMoMcInfo.getBudget();

            } else {
                rglog.error("不支持的营销活动类型<{}>,不可转换数据!", mcTypeOne);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }

            /* 营销活动编号 */
            moneyOffMcListInfoBean.setMcNo(mcNo);
            /* 营销活动名称 */
            moneyOffMcListInfoBean.setMcName(mcName);
            /* 营销活动类型 */
            moneyOffMcListInfoBean.setMcType(mcType);
            /* 活动起始日期 */
            moneyOffMcListInfoBean.setMcStartDate(mcStartDate);
            /* 活动截止日期 */
            moneyOffMcListInfoBean.setMcEndDate(mcEndDate);
            /* 活动结束日期 */
            moneyOffMcListInfoBean.setMcActualEndDate(mcActualEndDate);
            /* 出资方 */
            if (!StringUtil.isNullorEmpty(sponsor)) {
                moneyOffMcListInfoBean.setSponsor(sponsor);
            }
            /* 活动预算 */
            moneyOffMcListInfoBean.setBudget(budget);
            /* 已消耗预算 */
            moneyOffMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            /* 审批状态 */
            moneyOffMcListInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            moneyOffMcListInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            moneyOffMcListInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            moneyOffMcListInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            moneyOffMcListInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());
            /* 查询主体参加活动标志 */
            moneyOffMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_DEFAULT);

        } catch (Exception e) {
            rglog.error("转换营销活动临时表信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return moneyOffMcListInfoBean;
    }

}
